package com.kullachi.jmx.d3;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.management.MBeanOperationInfo;
import javax.media.j3d.Appearance;
import javax.media.j3d.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;

import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;

public class MBeanOperationInfo3D {
	Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    TransformGroup viewer;
    MBeanOperationInfo attrInfo;
    Sphere sphere ;
	public MBeanOperationInfo3D(MBeanOperationInfo info){
		super();
		this.attrInfo	= info;
	}

	public TransformGroup getViewer(Transform3D transform) {
		Appearance app = new Appearance();
		Color3f objColor = new Color3f(Color.DARK_GRAY);
		app.setMaterial(new Material(objColor, black, objColor, white, 80.0f));

		// Create name base image
		BufferedImage image = new BufferedImage(50, 50,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();

		// create a rectangle filled with the color white:
		g.setColor(new Color(46,139,87));
		g.fillRect(0, 0, 50, 50);

		// add some text
		//g.setColor(Color.RED);
		//g.drawString(attrInfo.getName(), 20, 20);
		app.setTexture(new TextureLoader(image).getTexture());
		sphere = new Sphere(1.0f, Sphere.GENERATE_NORMALS
		          | Sphere.GENERATE_TEXTURE_COORDS,app);

		viewer = new TransformGroup(transform);
		viewer.addChild(sphere);

		return viewer;
	}

	public void error() {
		Appearance app = new Appearance();
		Color3f objColor = new Color3f(Color.DARK_GRAY);
		app.setMaterial(new Material(objColor, black, objColor, white, 80.0f));

		// Create name base image
		BufferedImage image = new BufferedImage(50, 50,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();

		// create a rectangle filled with the color white:
		g.setColor(Color.RED);
		g.fillRect(0, 0, 50, 50);
		viewer.removeChild(sphere);
		sphere.setAppearance(app);
		viewer.addChild(sphere);
		
	}
}
